package com.softtown.modules.sys.controller;

import com.qcloud.cos.utils.StringUtils;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.security.user.UserDetail;
import com.softtown.modules.sys.dto.SysAppMenuDTO;
import com.softtown.modules.sys.dto.SysMenuDTO;
import com.softtown.modules.sys.dto.SysSetMenuDTO;
import com.softtown.modules.sys.enums.MenuTypeEnum;
import com.softtown.modules.sys.service.CompSysRoleUserService;
import com.softtown.modules.sys.service.SysCompMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: Dingyayong
 * @Date: 2019/5/31 10:51
 * @Description:
 */
@RestController
@RequestMapping("/comp/menu")
@Api(tags = "公司菜单管理")
public class CompSysMenuController {

    @Resource
    private SysCompMenuService sysCompMenuService;

    @Resource
    private CompSysRoleUserService compSysRoleUserService;

    @PostMapping("getAppList")
    @ApiOperation("获取APP信息")
    @LogOperation("获取APP信息")
    public Result<List<SysMenuDTO>> getAppList(@RequestBody Map<String, Object> parms) {
        Result result = new Result<>();
        boolean isNullOrEmpty = StringUtils.isNullOrEmpty(parms.get("companyId").toString().trim());
        if (isNullOrEmpty) {
            return result.error(ErrorCode.COMPANY_EMPTY_ERROR);
        }
        if (!RegularUtils.isNum(parms.get("companyId").toString().trim())) {
            return result.error(ErrorCode.INCORRECT_DATA_FORMAT);
        }
        List<SysMenuDTO> list = sysCompMenuService.getListApp(Long.parseLong(parms.get("companyId").toString()));
        return new Result<List<SysMenuDTO>>().ok(list);
    }

    @PostMapping("nav")
    @ApiOperation("导航")
    @LogOperation("导航")
    public Result<List<SysMenuDTO>> nav(@RequestBody SysSetMenuDTO sysSetMenuDTO) {
        UserDetail user = SecurityUser.getUser();
        List<Long> roleIds = compSysRoleUserService.getRoleId(sysSetMenuDTO.getCompanyId(), user.getId());
        List<SysMenuDTO> list = sysCompMenuService.getUserMenuList(user, MenuTypeEnum.MENU.value(), sysSetMenuDTO.getCompanyId(), sysSetMenuDTO.getAppId(), roleIds);
        return new Result<List<SysMenuDTO>>().ok(list);
    }

    @PostMapping("compMenu")
    @ApiOperation("公司菜单树形图")
    @LogOperation("公司菜单树形图")
    public Result<List<SysMenuDTO>> compMenu(@RequestBody String compId) {
        String id = JSONObject.fromObject(compId).get("compId").toString();
        List<SysMenuDTO> list = sysCompMenuService.getListTreeApp(Long.valueOf(id));
        return new Result<List<SysMenuDTO>>().ok(list);
    }



    @PostMapping("getAppMenuList")
    @ApiOperation("getAppMenuList")
    public Result<List<SysAppMenuDTO>> getAppMenuList(@RequestBody SysSetMenuDTO sysSetMenuDTO) {
        List<SysAppMenuDTO> list = sysCompMenuService.getAppMenuList( sysSetMenuDTO.getCompanyId());
        return new Result<List<SysAppMenuDTO>>().ok(list);
    }


}
